use clap::Parser;
use rcli::{
    CmdOpts, SubCommand, SubTextCommand, Subbase64Command, process_csv, process_decode,
    process_encode, process_generate_key, process_genpass, process_text_sign, process_text_verify,
};

fn main() -> anyhow::Result<()> {
    let args = CmdOpts::parse();

    // println!("{:?}", args);
    match args.cmd {
        SubCommand::Csv(opts) => {
            let output = if let Some(output) = opts.output {
                output.clone()
            } else {
                format!("output.{}", opts.format)
            };
            process_csv(&opts.input, output, opts.format)?
        }
        SubCommand::Genpass(opts) => {
            let pass_key = process_genpass(
                opts.length,
                opts.no_upcase,
                opts.no_lowercase,
                opts.no_number,
                opts.no_symbol,
            )?;
            println!("Genpass key: {}", pass_key)
        }
        SubCommand::Base64(subcmd) => match subcmd {
            Subbase64Command::Encode(opt) => {
                process_encode(&opt.input, opt.format)?;
            }
            Subbase64Command::Decode(opt) => {
                process_decode(&opt.input, opt.format)?;
            }
        },
        SubCommand::Text(subcmd) => match subcmd {
            SubTextCommand::Sign(opt) => {
                let signer = process_text_sign(&opt.input, &opt.key, opt.format)?;
                print!("Text sign ...: {}", signer);
            }
            SubTextCommand::Verify(opt) => {
                let verifer = process_text_verify(&opt.input, &opt.key, &opt.sig, opt.format)?;
                print!("Text verify ...{}", verifer);
            }
            SubTextCommand::GenerateKey(opt) => {
                let key = process_generate_key(opt.format)?;
                println!("generate key: {:?}", String::from_utf8(key[0].clone())?);
            }
        },
    };

    Ok(())
}
